//==================================================================
/// CppKazRace.cpp
///
/// Created by Davide Pasca - 2009/10/5
/// See the file "license.txt" that comes with this project for
/// copyright info. 
//==================================================================

#include "stdafx.h"
#include "CppKazRace.h"

//==================================================================
CppKazRace::CppKazRace()
{
	mLeftIsDown		= false;
	mRightIsDown	= false;
	mUpIsDown		= false;
	mDownIsDown		= false;

	mScreenD		= 0;

	mpoPO = new printout();

	mpoFont = new Font( "font1.png", 12, 16 );

	_laptimes.resize( 5 );
	for (size_t i=0; i < _laptimes.size(); ++i)
		_laptimes[i] = 0;

	// initial dummy size
	U32	w = 320;
	U32	h = 240;

	// setup offscreen
	mpoOffGfx = new graphics( w, h );
	mOffGfxIsBGR = false;

	mpoXFormer = new transformer( w, h, mScreenD );

	// setup horizon
	mpoHor = new horizon( w, h );
	mpoHor->colors_set(
				color( 80, 160, 230 ),
				color( 210, 220, 240 ),
				color( 220, 200, 180 ) );

	// ----
	load_stage();
}

//==================================================================
void CppKazRace::lap_add_new()
{
	++_n_laps;

	for (int i=(int)_laptimes.size()-1; i >= 1; --i)
		_laptimes[i] = _laptimes[i-1];

	_laptimes[0] = 0;
}

//==================================================================
void CppKazRace::reset_pos()
{
	mpoRoad->pos_set( 0 );

	mpoVehicles.clear_free();

	mpoVehicles.push_back( new vehicle( mpoStartFrame.get(), (150+-500), 150, 0, false ) );

	mpoVehicles.push_back( new vehicle( mpoRQueen1.get(), (150+-610), 100, 0, false ) );
	mpoVehicles.push_back( new vehicle( mpoRQueen2.get(), (150+-390), 100, 0, false ) );
	mpoVehicles.push_back( new vehicle( mpoRQueen3.get(), (150+-630), 50, 0, false ) );
	mpoVehicles.push_back( new vehicle( mpoRQueen4.get(), (150+-380), 50, 0, false ) );

	mpoVehicles.push_back( new vehicle( mpoF500.get(), 80, 0, 80 ) );

	int	stage_len = 2400;
	int	i;
	int	speed, x_off, road_pos;

	for (i=0; i < 30; ++i)
	{
		sprite	*pSprt;
	
		if ( (int)(Math::Random() * 100) <= 80 )
			pSprt = mpoF500.get();
		else
			pSprt = mpoMoo.get();

		speed = (int)(Math::Random() * 30) + 90;
		x_off = (int)(Math::Random() * 1100) + -550;
		road_pos = (int)(Math::Random() * stage_len) * 100;

		mpoVehicles.push_back( new vehicle( pSprt, x_off, road_pos, speed ) );
	}
}
